﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UI.Lib;
using System.IO;
using UI.Lib.Factory;
using UI.CommonForm;
using Com.Seezt.Skins;
using UI.ParentForm;

namespace UI
{
    public partial class FrmLogin : BasicForm
    {
        string guid = "";

        public FrmLogin()
        {
            InitializeComponent();
            this.picLogo.Width = this.Width - 20;
            this.Size = new Size(461, 290);
            Init();
        }

        private void Init()
        {
            this.lbUserName.Font = this.lbUserPwd.Font = this.btnLogin.Font = Global.AllFont;

            string verfilePath = string.Concat(Context.ApplicationPath, "\\", "ver.dat");
            //读取版本信息
            string ver = Components.CommonUtil.GetInstance.LoadFromFile(verfilePath);
            Context.Version = string.IsNullOrEmpty(ver) ? "未知版本" : ver;

            this.Text = string.Format("{0}{1}({2})", this.Text, Context.Version, DateTime.Now.ToShortDateString());
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUserName.Text))
            {
                MessageBox.Show("请录入用户帐号!", "提示");
                txtUserName.Focus();
                return;
            }
            else if (string.IsNullOrEmpty(txtUserPwd.Text))
            {
                MessageBox.Show("请录入用户密码!", "提示");
                txtUserPwd.Focus();
                return;
            }

            //清空tmp文件
            RemoveTmpFile();
            try
            {
                //检查更新
                AutoUpdaterHelper helper = new AutoUpdaterHelper();
                if (!helper.Update())
                {
                    MessageBox.Show("存在新文件，您没有更新，暂时无法登陆！", "提示");
                    return;
                }
            }
            catch (PsException ex)
            {
                MessageBox.Show(ex.ErrMsg, "错误提示");
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Hide();
        }

        /// <summary>
        /// 返回登录标识
        /// </summary>
        /// <returns></returns>
        public string GetLoginGuid()
        {
            return guid;
        }

        public string LoginUserName
        {
            get
            {
                return this.txtUserName.Text;
            }
        }

        public string LoginPwd
        {
            get
            {
                return this.txtUserPwd.Text;
            }
        }

        /// <summary>
        /// 清空tmp临时文件
        /// </summary>
        private void RemoveTmpFile()
        {
            DirectoryInfo info = new DirectoryInfo(Application.StartupPath);
            FileInfo[] files = info.GetFiles();
            foreach (FileInfo file in files)
            {
                if (file.Extension.ToLower().IndexOf("tmp") > -1)
                    file.Delete();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Components.LogHelper.Entity.Out("button1_Click" + System.Threading.Thread.CurrentThread.ManagedThreadId);
            UI.CommonForm.FrmWaitingBox f = new UI.CommonForm.FrmWaitingBox(DoSave,
             3, true, true);

            f.ShowDialog(this);
        }

        public string DoSave()
        {
            Components.LogHelper.Entity.Out("DoSave" + System.Threading.Thread.CurrentThread.ManagedThreadId);
            System.Threading.Thread.Sleep(1000 * 10);
            Components.LogHelper.Entity.Out("DoSave" + System.Threading.Thread.CurrentThread.ManagedThreadId);
            Components.LogHelper.Entity.Out(DateTime.Now.ToString());
            return "";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FrmMessage frm = new FrmMessage();
            frm.Show();
        }

        private void txtUserName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                txtUserPwd.Focus();

                e.Handled = true; //不发出提示音
            }
        }

        private void txtUserPwd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                btnLogin_Click(null, null);

                e.Handled = true; //不发出提示音
            }
        }

        private void FrmLogin_SizeChanged(object sender, EventArgs e)
        {
            this.Size = new Size(461, 290);
        }

        private void FrmLogin_FormClosed(object sender, FormClosedEventArgs e)
        {
        }
    }
}
